home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / str_sep.pro < prev    next >
Text File  |  1997-07-08  |  3KB  |  115 lines

  1. ; $Id: str_sep.pro,v 1.6 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1992-1997, CreaSo Creative Software Systems GmbH,
  4. ;    and Research Systems, Inc.  All rights reserved.
  5. ;    Unauthorized reproduction prohibited.
  6. ;+
  7. ; NAME:
  8. ;    STR_SEP
  9. ;
  10. ; PURPOSE:
  11. ;    This routine cuts a string into pieces which are separated by the 
  12. ;    separator string.
  13. ; CATEGORY:
  14. ;    String processing.
  15. ; CALLING SEQUENCE:
  16. ;    arr = STR_SEP(string, separator)
  17. ;
  18. ; INPUTS:
  19. ;    str - The string to be separated.
  20. ;    sep - The separator.
  21. ;
  22. ; KEYWORDS:
  23. ;    ESC = escape character.  Only valid if separator is a single character.
  24. ;        Characters following the escape character are treated
  25. ;        literally and not interpreted as separators.
  26. ;        For example, if the separator is a comma,
  27. ;        and the escape character is a backslash, the character
  28. ;        sequence 'a\,b' is a single field containing the characters
  29. ;        'a,b'.
  30. ;    REMOVE = if set, remove all blanks from fields.
  31. ;    TRIM = if set, remove only leading and trailing blanks from fields.
  32. ;
  33. ; OUTPUT: 
  34. ;    An array of strings as function value.
  35. ;
  36. ; COMMON BLOCKS:
  37. ;    None
  38. ;
  39. ; SIDE EFFECTS:
  40. ;    No known side effects.
  41. ;
  42. ; RESTRICTIONS:
  43. ;    None.
  44. ;
  45. ; EXAMPLE:
  46. ;    array = STR_SEP ("ulib.usca.test", ".")
  47. ;
  48. ; MODIFICATION HISTORY:
  49. ;    July 1992, AH,    CreaSo        Created.
  50. ;    December, 1994, DMS, RSI    Added TRIM and REMOVE.
  51. ;-
  52. function STR_SEP, s, sep, REMOVE = remove, TRIM = trim, ESC=esc
  53.  
  54.  
  55. ON_ERROR, 2
  56. spos = 0L
  57. if n_elements(esc) gt 0 then begin        ;Check for escape character?
  58.   if strpos(s, esc) lt 0 then goto, no_esc    ;None in string, use fast case
  59.   besc = (byte(esc))[0]
  60.   bsep = (byte(sep))[0]
  61.   new = bytarr(strlen(s)+1)
  62.   new[0] = byte(s)
  63.   j = 0L
  64.   for i=0L, n_elements(new)-2 do begin
  65.     if new[i] eq besc then begin
  66.     new[j] = new[i+1]
  67.     i = i + 1
  68.     endif else if new[i] eq bsep then new[j] = 1b $   ;Change seps to 1b char
  69.     else new[j] = new[i]
  70.     j = j + 1
  71.     endfor
  72.   new = string(new[0:j-1])
  73.   w = where(byte(new) eq 1b, count)  ;where seps are...
  74.   arr = strarr(count+1)
  75.   for i=0L, count-1 do begin
  76.     arr[i] = strmid(new, spos, w[i]-spos)
  77.     spos = w[i] + 1
  78.     endfor
  79.   arr[count] = strmid(new, spos, strlen(s))  ;Last element
  80.   goto, done
  81.   endif            ;esc
  82.  
  83. no_esc:
  84. if strlen(sep) le 1 then begin    ;Single character separator?
  85.     w = where(byte(s) eq (byte(sep))[0], count)  ;where seps are...
  86.     arr = strarr(count+1)
  87.     for i=0, count-1 do begin
  88.     arr[i] = strmid(s, spos, w[i]-spos)
  89.     spos = w[i] + 1
  90.     endfor
  91.     arr[count] = strmid(s, spos, strlen(s))  ;Last element
  92. endif else begin        ;Multi character separator....
  93.     n = 0L           ; Determine number of seperators in string.
  94.     repeat begin
  95.     pos = strpos (s, sep, spos)
  96.     spos = pos + strlen(sep)
  97.     n = n+1
  98.     endrep until pos eq -1
  99.  
  100.     arr = strarr(n)       ; Create result array
  101.     spos = 0L
  102.     for i=0L, n-1 do begin   ; Separate substrings
  103.       pos = strpos (s, sep, spos)
  104.       if pos ge 0 then arr[i] = strmid (s, spos, pos-spos) $
  105.       else arr[i] = strmid(s, spos, strlen(s))
  106.       spos = pos+strlen(sep)
  107.    endfor
  108. endelse
  109.  
  110. done:
  111. if keyword_set(trim) then arr = strtrim(arr,2) $
  112. else if keyword_set(remove) then arr = strcompress(arr, /REMOVE_ALL)
  113. return, arr
  114. end
  115.